package com.wanmait.hotelManagement.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.hotelManagement.dao.AdminRoomTypeDao;
import com.wanmait.hotelManagement.vo.RoomType;

/**
 * Servlet implementation class AdminRoomTypeServlet
 */
@WebServlet("/AdminRoomTypeServlet")
public class AdminRoomTypeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminRoomTypeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String action = request.getParameter("action");
		if(action==null)
		{
			this.init(request, response);
		}
		else if("addRoomType".equals(action))
		{
			this.addRoomType(request, response);
		}
	}
	
	protected void init(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		List<RoomType> allRoomType = AdminRoomTypeDao.getDao().getAllRoomType();
		request.setAttribute("allRoomType", allRoomType);
		request.getRequestDispatcher("/admin/room/roomType.jsp").forward(request, response);
	}

	protected void addRoomType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String roomName = request.getParameter("roomName");
		String remarks = request.getParameter("remarks");
		float roomPrice = Float.parseFloat(request.getParameter("roomPrice"));
		int personCount = Integer.parseInt(request.getParameter("personCount"));
		int roomCount = Integer.parseInt(request.getParameter("roomCount"));
		
		RoomType roomType = new RoomType();
		roomType.setPersonCount(personCount);
		roomType.setRemarks(remarks);
		roomType.setRoomCount(roomCount);
		roomType.setRoomName(roomName);
		roomType.setRoomPrice(roomPrice);
		
		AdminRoomTypeDao.getDao().addRoomType(roomType);
		System.out.println("ok");
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
